package com.ctg.behavior.calc.impala.builder.userpath;

import java.util.Date;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.impala.report.userpath.CtUserPathUserIdsReport;
import com.ctg.behavior.calc.utils.CtCalcPropertyUtils;
import com.ctg.behavior.common.calc.CtCalcPropertyType;
import com.ctg.behavior.common.consts.CtCalcConsts;
import com.ctg.behavior.common.userpath.CtUserPathCalcBody;
import com.ctg.behavior.utils.CtDateUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtUserPathUserIdsReportBuilder {

	public CtUserPathUserIdsReport doBuilder(CtUserPathCalcBody calcBody, String eventId, Integer columnIndex, String userIdsSQL) {

		CtUserPathUserIdsReport calcReport = this.copyBody(calcBody);
		calcReport.setTheEventId(eventId);
		calcReport.setTheColumnIndex(columnIndex);
		calcReport.setTheUserIdsSQL(userIdsSQL);
		if (CtCalcConsts.EVENT_INITIAL.equals(calcBody.getSourceType())) {
			// 源事件类型-开始事件
			calcReport.setInitEvent(true);
		} else {
			// 源事件类型-结束事件
			calcReport.setInitEvent(false);
		}

		CtCalcPropertyType theGroupByField = CtCalcPropertyUtils.findField(eventId, calcBody.getByFields());
		if (null != theGroupByField && !CtCalcConsts.MEASURE_TYPE_TIMES.equals(theGroupByField.getProperty())) {
			calcReport.setTheByProperty(theGroupByField.getProperty());
		}

		if (calcReport.getInitEvent()) {
			if (columnIndex <= 0) {
				calcReport.setTheColumnDate(calcBody.getFromDate());
			} else {
				Date theDay = CtDateUtils.stringToDate(calcBody.getFromDate(), CtDateUtils.YYYY_MM_DD);
				theDay = CtDateUtils.addDate(theDay, columnIndex);
				calcReport.setTheColumnDate(CtDateUtils.dateToString(theDay, CtDateUtils.YYYY_MM_DD));
			}
		} else {
			if (columnIndex <= 0) {
				calcReport.setTheColumnDate(calcBody.getToDate());
			} else {
				Date theDay = CtDateUtils.stringToDate(calcBody.getToDate(), CtDateUtils.YYYY_MM_DD);
				theDay = CtDateUtils.addDate(theDay, -columnIndex);
				calcReport.setTheColumnDate(CtDateUtils.dateToString(theDay, CtDateUtils.YYYY_MM_DD));
			}
		}

		log.info("calcReports={}", JSON.toJSONString(calcReport));

		return calcReport;

	}

	public CtUserPathUserIdsReport copyBody(CtUserPathCalcBody body) {
		CtUserPathUserIdsReport oneReport = new CtUserPathUserIdsReport();

		// CtgApplicationInfo
		oneReport.setAppId(body.getAppId());
		oneReport.setBookmarkId(body.getBookmarkId());
		oneReport.setRequestId(body.getRequestId());

		// CtCalcCommonBody
		oneReport.setFromDate(body.getFromDate());
		oneReport.setToDate(body.getToDate());
		oneReport.setUnit(body.getUnit());
		oneReport.setFilter(body.getFilter());
		oneReport.setSubTaskType(body.getSubTaskType());
		oneReport.setSubjectId(body.getSubjectId());

		// CtUserPathCalcBody
		oneReport.setByFields(body.getByFields());
		oneReport.setEventIds(body.getEventIds());
		oneReport.setSourceType(body.getSourceType());
		oneReport.setSourceEvent(body.getSourceEvent());
		oneReport.setSamplingFactor(body.getSamplingFactor());
		oneReport.setSessionInterval(body.getSessionInterval());
		oneReport.setColLimit(body.getColLimit());
		oneReport.setRowLimit(body.getRowLimit());

		return oneReport;
	}

}
